package com.bdd.controller;

import com.bdd.Result.Result;
import com.bdd.model.bo.clothing.ClothingForOrderItemBO;
import com.bdd.service.OrderItemService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author huanghaining
 * @date 2022/3/18
 */
@RestController
@RequestMapping("/orderItem")
@Api(value = "订单项管理")
public class OrderItemController {
    @Autowired
    OrderItemService orderItemService;

    @GetMapping("/list")
    @ApiOperation("订单项查找")
    @RequiresRoles(value={"STAFF","ADMIN"},logical= Logical.OR)
    @ResponseBody
    public Result<List<ClothingForOrderItemBO>> list() {
        List<ClothingForOrderItemBO> orderItemBOList = orderItemService.list();
        return Result.ok(orderItemBOList);
    }

    @PostMapping("/add")
    @ApiOperation("订单项增加")
    @ResponseBody
    @RequiresRoles(value={"STAFF","ADMIN"},logical= Logical.OR)
    public Result add(Integer id,String sizeStr) {
        if (Objects.isNull(id)) {
            throw new IllegalArgumentException("商品不可为空");
        }
        orderItemService.addToCache(id,sizeStr);
        return Result.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除订单项")
    @ResponseBody
    @RequiresRoles(value={"STAFF","ADMIN"},logical= Logical.OR)
    public Result delete(Integer id) {
        if (Objects.isNull(id)) {
            throw new IllegalArgumentException("订单项id不可为空");
        }
        orderItemService.delete(id);
        return Result.ok();
    }


    @PostMapping("/settlement")
    @ApiOperation("下单")
    @ResponseBody
    @RequiresRoles(value={"STAFF","ADMIN"},logical= Logical.OR)
    public Result<Map<String,Integer>> settlement(Integer userId) {
        Integer orderId = orderItemService.settlement(userId);

        Map<String, Integer> map = new HashMap<>();
        map.put("orderId",orderId);
        return Result.ok(map);
    }

    @PostMapping("/pay")
    @ApiOperation("支付")
    @ResponseBody
    @RequiresRoles(value={"STAFF","ADMIN"},logical= Logical.OR)
    public Result pay(Integer orderId) {
        orderItemService.pay(orderId);
        return Result.ok();
    }


}
